<?php
class Back_SaisonsController extends Osml_Controller_Admin_Action
{

    public function indexAction()
    {
        $model = new Back_Model_Saisons();
        $select = new Zend_Db_Table_Select($model->_getMapper()->_getTable());
        
        $form = null;
        
        $this->view->tableIdField = 'id_saison';
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
            'id_saison' => array(
                'name' => 'id', 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'nom' => array(
                'name' => __("Nom"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'ouverte' => array(
                'name' => __("Ouverte"), 
                'link' => "editer"
            ), 
            'date_debut' => array(
                'name' => __("Début"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'date_fin' => array(
                'name' => __("Fin"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'defaut' => array(
                'name' => __("Défaut"), 
                'link' => array(
                    'action' => 'editer'
                )
            )
        );
        
        /*
         * Thierry BROUARD (BIIS) callback = array( $objet, $method ) link =
         * array( action [,controller[,module]] ) ou array( 'callback' => array(
         * $objet, $method ) )
         */
        $this->view->tableOptions = array(
            'callback' => array(
                $this, 
                '_transformRowsCallback'
            ),  // Transformation callback
            'link' => array(
                'action' => 'voir'
            ), 
            'pageNumber' => (int) $this->_getParam('page', 1), 
            'pageItemPerPage' => (int) $this->_getParam('par', 10), 
            'form' => $form
        );
    }

    public function editerAction()
    {
        $requete = $this->getRequest();
        $id = $requete->getParam("id", 0);
        
        $form = new Back_Form_Saisons('Formulairejndpro');
        $model = new Back_Model_Saisons($id);

        $membres = array();
        
        if ($requete->isPost()) {
            if ($form->isValid($requete->getPost())) {
                if (intval($id) > 0) {
                    $model->charger($id);
                    $model->setOptions($form->getValues());
                } else {
                    $model->setOptions($form->getValues());
                }
                $resultat = $model->sauver();
                $id = $model->getId();
                
                $this->redirect("index");
            }
        } elseif ($requete->isGet()) {
            if ($id > 0) {
                $model->charger($id);
                $form->populate($model->toArray());
                
                //    Récupération des membres de la saison
                $membres = Back_Model_UtilisateurSaison::recuperationUtilisateurParSaison($id);
            }
        }
        $this->view->form = $form;
        $this->view->model = $model;
        $this->view->membres = $membres;
    }

    public function supprimerAction()
    {
        $requete = $this->getRequest();
        $id = $requete->getParam("id", 0);
        
        $model = new Back_Model_Saisons($id);
        $model->supprimer($id);
        
        $this->redirect("index");
    }

    /**
     *
     * @see Jnd_Controller_Action::_transformRowsCallback()
     */
    public function _transformRowsCallback( $row, $view )
    {
        if (isset($row["ouverte"])) {
            $row['ouverte'] = ($row['ouverte'] ? __("oui") : __("non"));
        }
    }
}

